//
//  KYNSString+ImageUrl.m
//  ANG
//
//  Created by fuxiang on 15/10/22.
//  Copyright (c) 2015年 aniuge. All rights reserved.
//

#import "KYNSString+ImageUrl.h"

//是否为空或是[NSNull null]
#define NotNilAndNull(_ref)  (((_ref) != nil) && (![(_ref) isEqual:[NSNull null]]) && (![(_ref) isEqual:@"null"]) && (![(_ref) isEqual:@"(null)"]))

#define IsNilOrNull(_ref)   (((_ref) == nil) || ([(_ref) isEqual:[NSNull null]]) || ([(_ref) isEqual:@"null"]) || ([(_ref) isEqual:@"(null)"]))

//字符串是否为空
#define IsStrEmpty(_ref)    (((_ref) == nil) || ([(_ref) isEqual:[NSNull null]]) ||([(_ref)isEqualToString:@""]))

//数组是否为空
#define IsArrEmpty(_ref)    (((_ref) == nil) || ([(_ref) isEqual:[NSNull null]]) ||([(_ref) count] == 0))

@implementation NSString (ImageUrl)

+ (NSString *)stringWithImageUrl:(NSString *)imageUrl imageWidth:(NSInteger)imageWidth imageHeight:(NSInteger)imageHeight
{
    
    if (NotNilAndNull(imageUrl) && !IsStrEmpty(imageUrl))
    {
        NSString *imageSizeStr = [NSString stringWithFormat:@"_%ld_%ld",(long)imageWidth,(long)imageHeight];
        
        NSMutableString *imageStr = [[NSMutableString alloc] initWithFormat:@"%@", imageUrl];
        [imageStr insertString:imageSizeStr atIndex:imageStr.length-4];
        
        return imageStr;
    }
    return @"";
}
+ (NSMutableDictionary*)getURLParametersFromString:(NSString *)urlStr {
    NSRange  range = [urlStr rangeOfString:@"?"];
    if(range.location==NSNotFound) return  nil;
    NSMutableDictionary  *params = [NSMutableDictionary   dictionary];
    NSString  *parametersString = [urlStr substringFromIndex:range.location+1];
    if([parametersString containsString:@"&"]) {
        NSArray  *urlComponents = [parametersString componentsSeparatedByString:@"&"];
        for(NSString *keyValuePair in urlComponents) {
            NSArray *pairComponents = [keyValuePair componentsSeparatedByString:@"="];
            NSString *key = [pairComponents.firstObject stringByRemovingPercentEncoding];
            NSString*value = [pairComponents.lastObject  stringByRemovingPercentEncoding];
            if(key==nil|| value ==nil) continue;
            id existValue = [params valueForKey:key];
            if(existValue !=nil) {
                if([existValue  isKindOfClass:[NSArray  class]]) {
                    NSMutableArray*items = [NSMutableArray arrayWithArray:existValue];
                    [items addObject:value];
                    [params setValue:items forKey:key];
                }else{
                    [params setValue:@[existValue,value]forKey:key];
                }
            }else{
                [params setValue:value forKey:key];
            }
        }
    }else{
        NSArray *pairComponents = [parametersString  componentsSeparatedByString:@"="];
        if(pairComponents.count==1) return nil;
        NSString *key = [pairComponents.firstObject  stringByRemovingPercentEncoding];
        NSString *value = [pairComponents.lastObject  stringByRemovingPercentEncoding];
        if(key ==nil|| value ==nil) return nil;
        [params setValue:value forKey:key];
    }
    return params;
}
@end
